/**
* @Title: insuredBlacklist_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
* 审核岗审核事件的时候对选择分配人的弹出框
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年10月29日 下午14:46:06
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('auditFindDispatchSelectUserModalCtrl', ['$scope', '$http','BaseURL', 'Modal','$modalInstance','CaseAuditFindDispathService','CaseCommonService', function($scope, $http, baseUrl, Modal,$modalInstance,CaseAuditFindDispathService,CaseCommonService){
		
		
		
		
		
	
		$scope.userInfoListA=[];
		$scope.userInfoListB=[];
		
		function initRoleUserInfo(){
			var parameter={};
			CaseCommonService.loadUserInfoByRoleName(parameter).success(function(response){
		    	
		    	     var list=response.data;
		    	     
		    	     $.each(list,function(index,item){
		    	    	 var username=item.username;
		    	    	 var displayName=item.displayName;
		    	    	 var obj={
		    	    			 code:username,
		    	    			 name:username+"  "+displayName
		    	    	 };
		    	    	 
		    	    	 $scope.userInfoListA.push(obj);
		    	    	 $scope.userInfoListB.push(obj);
		    	     });
		    	     
				});
		}
		
		//初始化数据
		initRoleUserInfo();
		
		/***
		 * 生成经办岗A 的随机code
		 */
		function getARandomNum(){
			var listSize=$scope.userInfoListA.length;
			
		    var randNum=Math.floor(Math.random()*listSize);
		 
		   return $scope.userInfoListA[randNum].code;
			
		}
		
		/***
		 * 生成经办岗B 的随机code
		 */
		function getBRandomNum(userCodeA){
			var array=new Array();
			var listSize=$scope.userInfoListB.length;
			
		    for(var i=0;i<listSize;i++){
		    	var currentUserCode=$scope.userInfoListB[i].code;
		    	if(currentUserCode!=userCodeA){
		    		array.push(currentUserCode);
		    	}
		    }
		    var currentListSize=array.length;
		    var randNum=Math.floor(Math.random()*currentListSize);
		   return array[randNum];
			
		}
		
	
		//关闭model
		$scope.onClose = function() {
			$modalInstance.close();
		}
		
		/**
		 * 分配确定事件
		 */
		 $scope.loading = false;
		$scope.dispatchConfirm = function() {
			var selected = $("#userInfomations").val();
			if (!selected) {
				Modal.alert("请选择经办A岗人员");
				return;
			}else if(selected.length>1){
				Modal.alert("只能选择一位经办A岗人员");
				return;
			}
			
			var selectBopsition=$("#userInfomationsB").val();
			if(!selectBopsition){
				Modal.alert("请选择经办B岗人员");
				return;
			}else if(selectBopsition.length>1){
				Modal.alert("只能选择一位经办B岗人员");
				return;
			}
			
			if(selected[0]==selectBopsition[0]){
				Modal.alert("经办岗A和B不能是同一人!");
				return;
			}
			
			var parameter={
					userCode:selected[0],
					userBCode:selectBopsition[0],
			        problemNo:$scope.problemNo,
			        opinion:$scope.auditObj.opinion,
					opinionDesc:$scope.auditObj.opinionDesc,
					firstOpinionDesc:$scope.treatmentAdvice.opinionDesc,
                    warnMarkTempDetails:$scope.warnMarkTempDetails
			}
		
			 $scope.loading = true;
			CaseAuditFindDispathService.dispatchUser(parameter).success(function(response){
				           var currentFlag=response.state;
				           if(currentFlag=='true'){
				        	 $scope.loading = false;
				        	   $modalInstance.close();
				        	   $scope.saveReallyStaffAndInsuarnceInfo();
				        	   $scope.dispatchBackClick();
				           }else {
				        	   console.log(response.msg);
				           }
		                   
				});
			
			
			
		 }
		
		  
		
		/**
		 * 随机选择
		 */
		$scope.randDispatchConfirm = function(){
			var userACode = getARandomNum();
			var userBCode = getBRandomNum(userACode);
			$scope.selectUserACode = userACode;
			$scope.selectUserBCode = userBCode;
			$("#userInfomations").val(userACode);
			$("#userInfomationsB").val(userBCode);
		}	
		
		
		
       
       
 
		
	}]);
})();
